home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python-1.4 / Docs / Regular_Expressions_3.txt < prev   
Text File  |  1998-06-24  |  4KB  |  104 lines

  1. From cs.vu.nl!sun4nl!EU.net!enews.sgi.com!decwrl!pagesat.net!news.inconnect.com!news Thu Oct  3 16:44:08 1996
  2. Xref: cs.vu.nl comp.lang.python:13720
  3. Path: cs.vu.nl!sun4nl!EU.net!enews.sgi.com!decwrl!pagesat.net!news.inconnect.com!news
  4. From: "jeffrey P. Shell" <jeff@cynapses.com>
  5. Newsgroups: comp.lang.python
  6. Subject: Re: regular expressions
  7. Date: Wed, 02 Oct 1996 07:54:19 -0600
  8. Organization: Cynapses / riverYard
  9. Lines: 84
  10. Message-ID: <3252740B.6B49@cynapses.com>
  11. References: <3251FB33.2781E494@tube.eccosys.com>
  12. NNTP-Posting-Host: slc-dial-23.inconnect.com
  13. Mime-Version: 1.0
  14. Content-Type: text/plain; charset=us-ascii
  15. Content-Transfer-Encoding: 7bit
  16. X-Mailer: Mozilla 2.02 (Macintosh; I; 68K)
  17. To: Adam Bjornholm <adam@tube.eccosys.com>
  18.  
  19. > Anyone know where I can get an explaination of regular expressions?
  20.  
  21. there's a really really good QuickReference document that contains lots 
  22. of good information on Python..  One of the places you can read the 
  23. entire document is <http://www.cynapses.com/ry/python/pyQuickRef.html>.  
  24. Appendix 3 of "internet programming with Python" is online at 
  25. www.python.org somewhere and explains regular expressions in Python.
  26. -- 
  27. .jPS || jeff@cynapses.com || http://www.cynapses.com/ry/
  28.  
  29. from the Quick Reference (it's recommended you find that section of the 
  30. book described above, it has plenty of examples):
  31.  
  32.  
  33.                                       regex
  34.  
  35. Patterns are specified as strings. Default syntax is emacs-style. 
  36.  
  37. Special Characters (using default syntax): 
  38.  
  39. .       matches any character
  40. *       0 or more of preceeding regular expresssion
  41. +       1 or more of preceeding regular expresssion
  42. ?       0 or 1 of preceeding regular expresssion
  43. [ ]     defines character set: '[a-zA-Z]' to match all letters
  44. [^ ]    defines complemented character set: matches if char is NOT in 
  45. set
  46. ^       matches empty str at beginning of line
  47. $       matches empty str at end of line
  48. \       quoting char: \[ matches char '['
  49. \\      matches '\'; due to Python string rules, write as '\\\\' in
  50.         the pattern string.
  51. \|      specifies alternative: 'foo\|bar' matches 'foo' or 'bar'
  52. \( \)   grouping (for \|, or complicated expr, or substr for future
  53.         reference by \D character or group() method)
  54. \D      D is digit: matches substr matched by D'th \( \) in pattern
  55. \`      empty str at beginning of file
  56. \'      empty str at end of file
  57. \b      empty str at beg or end of word: '\bis\b' matches 'is', but not 
  58. 'his'
  59. \B      empty str NOT at beginning or end of word
  60. \<      empty str at beginning of word
  61. \>      empty str at end of word
  62. \w      any word constituent
  63. \W      any non-word constituent
  64.  
  65. Variables: 
  66.  
  67. error                   -- Exception when pattern string isn't valid 
  68. regexp.
  69.  
  70. Functions:
  71.  
  72. match(pattern, string)  -- Return how many characters at the beginning
  73.                            of <string> match regexp <pattern>
  74.                            or -1 if none.
  75.  
  76. search(pattern, string [, pos]) 
  77.                         -- Return the first position in <string> that
  78.                            matches regexp <pattern>.  Return -1 if none.
  79.                            [starting at <pos>.]
  80.  
  81. compile(pattern [,translate])
  82.                         -- Create regexp object that has methods
  83.                            match() and search() working as above. Also
  84.                            group(i1, [,i2]*). Also regs,  tuple of 
  85.                            positions matched; regs[0] is whole match, 
  86.                            next are subexpressions. E.g.
  87.                            p = compile('id\([a-z]\)\([a-z]\)')
  88.                            p.match('idab') ==> 4
  89.                            p.group(1, 2) ==> ('a', 'b')
  90.                            p.regs ==> ((0, 4), (2, 3), (3, 4), ...)
  91.  
  92. set_syntax(flag)        -- Set syntax flags for future calls to
  93.                            match(), search() and compile(). Returns
  94.                            current value. Flags in module regex_syntax.
  95.  
  96. symcomp(pattern [,translate])
  97.                         -- Like compile but with symbolic group
  98.                            names. Names in angle brackets. Access 
  99.                            through group method. E.g. 
  100.                            p = symcomp('id\(<l1>[a-z]\)\(<l2>[a-z]\)')
  101.                            p.match('idab') ==> 4
  102.                            p.group('l1') ==> 'a'
  103.  
  104.